﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GEA
{
    public partial class frmProcesso : Form
    {
        clDatabase dal = new clDatabase();
        public Entreposto Ent;
        

        public frmProcesso( Entreposto ent)
        {
            InitializeComponent();
            Ent = ent;
            GlobalVar.GlobalEntreposto = ent.Id_entreposto;
        }

        public frmProcesso()
        {
            InitializeComponent();
        }
        private void frmProcesso_Load(object sender, EventArgs e)
        {
            Load_Processos();
            dtpProcesso.CustomFormat = "yyyy-MM-dd";
            dtpProcesso.Format = DateTimePickerFormat.Custom;
            dtpContraMarca.CustomFormat = "yyyy-MM-dd";
            dtpContraMarca.Format = DateTimePickerFormat.Custom;

            
            lStocks.Visible = true;
            //lStocks.Location = new Point(lStocks.Location.X, (pStocks.Location.Y - 30));
            //((mdiMain)this.ParentForm).tsbImprimir.Visible = true;

            //cores das grid's
            dgvProcessos.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
            dgvProcessos.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvProcessos.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
            //dgvProcessos.Columns[0].HeaderCell.Style.Font = new Font("Arial", 14F, FontStyle.Bold, GraphicsUnit.Pixel);

            //cores das grid's
            dgvStocks.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
            dgvStocks.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvStocks.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
            //dgvStocks.Columns[0].HeaderCell.Style.Font = new Font("Arial", 14F, FontStyle.Bold, GraphicsUnit.Pixel);

            int h_2 = (((mdiMain)this.ParentForm).Height - 200 ) / 2;

            pnlEntrepostos.Height = h_2 - 20;
            dgvProcessos.Height = h_2 - 70;

            pStocks.Height = h_2;
            pStocks.Location = new Point(pnlEntrepostos.Location.X, h_2);
            lStocks.Location = new Point(pnlEntrepostos.Location.X, 10);
            dgvStocks.Height = h_2 - 50;

            this.Padding = new System.Windows.Forms.Padding(5, 20, 20, 50);

        }


        private void dgvProcessos_MouseClick(object sender, MouseEventArgs e)
        {
            Load_Stoks();       
        }
        

        public void Load_Processos()
        {

            
            ArrayList Lista_proc = new ArrayList();

            dgvProcessos.DataSource = dal.Processos_Get_Lista_dt(Ent.Id_entreposto);
            if (int.Parse(GlobalVar.GlobalValue.ToString()) != 0)
            {
                dgvProcessos.Columns["id_user"].Visible = false;
                dgvProcessos.Columns["data_criacao"].Visible = false;
                dgvProcessos.Columns["data_alteracao"].Visible = false;
                dgvProcessos.Columns["id_entreposto"].Visible = false;
                dgvProcessos.Columns["activo"].Visible = false;
                dgvProcessos.Columns["guid"].Visible = false;
            }
            else
            {
                dgvProcessos.Columns["id_user"].HeaderText = "UTILIZADOR".ToUpper();
                dgvProcessos.Columns["data_criacao"].HeaderText = "Data Criação".ToUpper();
                dgvProcessos.Columns["data_alteracao"].HeaderText = "Data Alteração".ToUpper();
                dgvProcessos.Columns["id_entreposto"].HeaderText = "Nº Entreposto".ToUpper();
                dgvProcessos.Columns["activo"].HeaderText = " Activo".ToUpper();
            }
            dgvProcessos.Columns["id_processo"].HeaderText = "Nº Processo".ToUpper();
            dgvProcessos.Columns["data_processo"].HeaderText = "Data".ToUpper();
            dgvProcessos.Columns["fornecedor"].HeaderText = "Fornecedor".ToUpper();
            dgvProcessos.Columns["id_factura"].HeaderText = "Nº Factura".ToUpper();
            dgvProcessos.Columns["contra_marca"].HeaderText = "Nº Contra Marca".ToUpper();
            dgvProcessos.Columns["data_contra_marca"].HeaderText = "Data Contra Marca".ToUpper();

            if (dgvProcessos.RowCount > 0)
            {
                Load_Stoks();
                ((mdiMain)this.ParentForm).tsbNovo.Enabled = true;
                ((mdiMain)this.ParentForm).tsbAlterar.Enabled = true;
                ((mdiMain)this.ParentForm).tsbApagar.Enabled = true;
            }
            else
            {
                dgvStocks.DataSource = null;
                ((mdiMain)this.ParentForm).tsbAlterar.Enabled = false;
                ((mdiMain)this.ParentForm).tsbApagar.Enabled = false;
                ((mdiMain)this.ParentForm).tsbNovo.Enabled = true;
            }
        }
        public void Load_Stoks()
        {

            DataGridViewCell cell = null;
            foreach (DataGridViewCell selectedCell in dgvProcessos.SelectedCells)
            {
                cell = selectedCell;
                break;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                string id_p = row.Cells["id_processo"].Value.ToString();
                dgvStocks.DataSource = dal.Mercadorias_Get_Lista_Stock_dt(Ent.Id_entreposto, id_p);
                dgvStocks.Columns["id_referencia"].HeaderText = "Referência".ToUpper();
                dgvStocks.Columns["id_referencia"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                dgvStocks.Columns[2].Visible = false;
                dgvStocks.Columns["Peso_Bruto"].HeaderText = "Peso Bruto".ToUpper();
                dgvStocks.Columns["Peso_Liquido"].HeaderText = "Peso Liquido".ToUpper();

                dgvStocks.Columns["valor"].DefaultCellStyle.Format = "N2";
                dgvStocks.Columns["peso_bruto"].HeaderText = "PESO BRUTO".ToUpper();
                dgvStocks.Columns["peso_bruto"].DefaultCellStyle.Format = "N2";
                dgvStocks.Columns["peso_liquido"].HeaderText = "PESO LIQUIDO".ToUpper();
                dgvStocks.Columns["peso_liquido"].DefaultCellStyle.Format = "N2";

                dgvStocks.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
            

        }


        private void dgvProcessos_MouseDoubleClick(object sender, MouseEventArgs e)
        {


            Processo proc = new Processo();

            string entreposto = "";
            string processo = "";

            DataGridViewCell cell = null;
            foreach (DataGridViewCell selectedCell in dgvProcessos.SelectedCells)
            {
                cell = selectedCell;
                break;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                entreposto = row.Cells["id_entreposto"].Value.ToString();
                processo = row.Cells["id_processo"].Value.ToString();

            }

            proc = dal.Processo_Get(entreposto, processo);

            Boolean du_entrada = dal.DU_Get_Tipo(entreposto, processo);
            if (du_entrada)
                ((mdiMain)this.ParentForm).startup_dus(proc, "ver");
            else
            {
                ((mdiMain)this.ParentForm).accao = "novo";
                ((mdiMain)this.ParentForm).startup_dus(proc, "novo");
                ((mdiMain)this.ParentForm).tsbNovo.Enabled = false;
                ((mdiMain)this.ParentForm).tsbAlterar.Enabled = false;
                ((mdiMain)this.ParentForm).tsbSalvar.Enabled = true;
                ((mdiMain)this.ParentForm).tsbCancelar.Enabled = true;
                ((mdiMain)this.ParentForm).tsbApagar.Enabled = false;
                ((mdiMain)this.ParentForm).tsbImprimir.Enabled = false;
            }
            



        }

        private void tbPesqProcesso_TextChanged(object sender, EventArgs e)
        {
            dgvProcessos.DataSource = dal.Processos_Get_Proc_Lista_dt(Ent.Id_entreposto, tbPesqProcesso.Text);
        }
        private void tbPesqRef_TextChanged(object sender, EventArgs e)
        {
            dgvStocks.DataSource = dal.Mercadorias_Get_Lista_Ref_dt(Ent.Id_entreposto, tbPesqRef.Text);

            dgvStocks.Columns["id_referencia"].HeaderText = "Referência".ToUpper();
            dgvStocks.Columns["stock"].HeaderText = "Stock".ToUpper();
            dgvStocks.Columns["valor"].HeaderText = "Valor".ToUpper();
            dgvStocks.Columns["peso_bruto"].HeaderText = "Peso Bruto".ToUpper();
            dgvStocks.Columns["peso_liquido"].HeaderText = "Peso Liquido".ToUpper();

            dgvStocks.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            
        }

        private void dgvStocks_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            //Mercadoria Merc = new Mercadoria();

            //DataGridViewCell cell = null;
            //foreach (DataGridViewCell selectedCell in dgvStocks.SelectedCells)
            //{
            //    cell = selectedCell;
            //    break;
            //}
            //if (cell != null)
            //{
            //    DataGridViewRow row = cell.OwningRow;
            //    Merc.Id_entreposto = Ent.Id_entreposto;
            //    Merc.Id_Referencia = row.Cells[0].Value.ToString();

            //}
            //frmrptSaldoReferencia R = new frmrptSaldoReferencia(Merc);
            //R.MdiParent = this.ParentForm;
            //R.Dock = DockStyle.Fill;
            //R.Show();
            
        }

        private void dgvProcessos_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.HeaderText.ToUpper();
        }

        private void dgvStocks_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.HeaderText.ToUpper();
        }

        
    }
}
